<?php
/**
 * Automatic action for lets manager
 * -Send expiration mail for exchanges with confirmation before delete
 * -Remove expired exchanges
 * @package    letsManager
 * @subpackage task
 * @author     Jerome Fouilloy
 * @version    SVN: $Rev$
 */
class pwLetsManagerPluginNightTask extends sfBaseTask
{
  /**
   * @see sfTask
   */
  protected function configure()
  {
    /* $this->addArguments(array(
     new sfCommandArgument('username', sfCommandArgument::REQUIRED, 'The user name'),
     new sfCommandArgument('group', sfCommandArgument::REQUIRED, 'The group name'),
     ));*/

    $this->addOptions(array(
    new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', 'backend'),
    new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
    new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),

    ));

    $this->namespace = 'pwLetsManagerPlugin';
    $this->name = 'night-tasks';
    $this->briefDescription = 'Do automatic actions (asks expired)';

    $this->detailedDescription = <<<EOF
The [pwLetsManagerPlugin:night-tasks] launch automatic action for lets manager
-Notify asks expired to user (only if the duration is always)
-Clean asks expired automatically
EOF;
  }

  /**
   * @see sfTask
   */
  protected function execute($arguments = array(), $options = array())
  {
    $databaseManager = new sfDatabaseManager($this->configuration);
    // $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
    $context = sfContext::createInstance($this->configuration);


    /* if (!$user)
     {
     throw new sfCommandException(sprintf('User "%s" does not exist.', $arguments['username']));
     }*/

    $associations = AssociationTable::getInstance()->findAll();
    foreach($associations as $asso)
    {
      $this->logSection('letsManager', sprintf('Night tasks for association %s', $asso->getName()));
      $this->execForAssociation($asso);
    }
  }
  
}